package com.ybxx.core.convert;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.ybxx.core.convert.DropDownNode;

/*公共方法*/
public class Convert {
	/*id--DropDownNode类中id属性值在源对象的那个属性存储
	 * text--DropDownNode类中text属性值在源对象的那个属性存储*/
	public static List convert(List source,String id,String text){
		List dest=new ArrayList();
		/*dropDownNode.setId(user.getId);
		 * dropDownNode.setText(user.getName);
		 * */
		for(Iterator iter=source.iterator();iter.hasNext();){
			Object obj=iter.next();
			Class ct=obj.getClass(); 
			Method method=null;
			String tmpId="";
			String tmpName="";
			DropDownNode property =new DropDownNode();
			try {
				method=ct.getMethod(getMethodName(id), null);
				tmpId = (String) method.invoke(obj, null);
				property.setId(tmpId);
				method=ct.getDeclaredMethod(getMethodName(text), null);
				tmpName=(String)method.invoke(obj,null);
				property.setText(tmpName);
			} catch (Exception e) {
				e.printStackTrace();
			} 
			dest.add(property);
		}
		return dest;
	}
	public static List convert(List source,String[] sourceProp,String[] destProp,Class destClass){
		List dest=new ArrayList();
		Class classSource=null;
		Method sourceMethods[]=null;
		Method destMethods[]=null;
		Object destObj=null;
		int currentPropIndex=-1;
		int i=0;
		for(Iterator iter=source.iterator();iter.hasNext();){
			Object obj=iter.next();
			if(i==0){
				classSource=obj.getClass();
				sourceMethods=classSource.getDeclaredMethods();
				i++;
			}
			destMethods=destClass.getDeclaredMethods();
			try {
				destObj=destClass.newInstance();//创建目的对象，等价于 user=new User();
				/*对象开始复制*/
				for(Method sourceMethod:sourceMethods){
					String sourceMethodName=sourceMethod.getName();
					if(sourceMethodName.startsWith("get")){
//						System.out.println("sourceMethodName="+sourceMethodName.substring(3));
						/*判断该属性是否需要复制*/
						for(int m=0;m<sourceProp.length;m++){
							if(sourceMethodName.substring(3).toLowerCase().equals(((sourceProp)[m]).toLowerCase())){
								currentPropIndex=m;
								break;
							}
						}
						/**取得目标对象属性*/
						if(currentPropIndex!=-1){
							String tmp=destProp[currentPropIndex];
							for(Method destMethod:destMethods){
								String destMethodName=destMethod.getName();
//								System.out.println("destMethodName="+destMethodName.substring(3));
								if(destMethodName.startsWith("set") && destMethodName.substring(3).toLowerCase().equals(tmp.toLowerCase())){
									destMethod.invoke(destObj, sourceMethod.invoke(obj));
									currentPropIndex=-1;
									break;
								}
							}
						}
					}
					
				}
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dest.add(destObj);
		}
		return dest;
	}
	//根据属性变量返回getX方法
	private static String getMethodName(String fildeName){
		 byte[] items = fildeName.getBytes();
		 items[0] = (byte)((char)items[0]-'a'+'A');;
		 return "get"+new String(items);
   }
}

